package com.qef.com.mmovie.api;

import com.avos.avoscloud.AVCloudQueryResult;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.CloudQueryCallback;
import com.avos.avoscloud.CountCallback;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.qef.com.mmovie.entity.DataBean;
import com.qef.com.mmovie.entity.LaughImgEntity;
import com.qef.com.mmovie.entity.User;
import com.qef.com.mmovie.entity.VVIdeoDetailsEntity;
import com.qef.com.mmovie.utils.Constants;
import com.qef.com.mmovie.utils.GsonUtils;

/**
 * 2016 逛街购（北京）网络科技有限公司，版权所有
 * guangjiegou - Android客户端
 * Context:云服务器数据管理
 * Created by fanenqian on 2017/6/6 17:20
 */
public class NetApi {
    public static void collection(DataBean dataBean, SaveCallback saveCallback) {
        AVObject avObject = new AVObject(Constants.Collection.FORM_COLLECTION);
        avObject.put(Constants.Collection.INFOID, dataBean.getId());
        avObject.put(Constants.Collection.VIDEO_TYPE, Constants.Collection.VIDEO_1);
        avObject.put(Constants.Collection.VIDEO_INFO, GsonUtils.beanToJson(dataBean));
        avObject.put(Constants.Collection.USER_ID, User.getCurrenUser().getUsername());
        avObject.saveInBackground(saveCallback);
    }

    public static void collection(VVIdeoDetailsEntity dataBean, SaveCallback saveCallback) {
        AVObject avObject = new AVObject(Constants.Collection.FORM_COLLECTION);
        avObject.put(Constants.Collection.INFOID, Integer.parseInt(dataBean.getData().getPostid()));
        avObject.put(Constants.Collection.VIDEO_TYPE, Constants.Collection.VIDEO_2);
        avObject.put(Constants.Collection.VIDEO_INFO, GsonUtils.beanToJson(dataBean));
        avObject.put(Constants.Collection.USER_ID, User.getCurrenUser().getUsername());
        avObject.saveInBackground(saveCallback);
    }

    public static void comment(int id, String content, SaveCallback saveCallback) {
        AVObject commInfo = new AVObject("Commont");
        commInfo.put("infoid", id);
        commInfo.put("content", content);
        commInfo.put("userName", User.getAlias());
        commInfo.put("userHead", User.getHead());
        commInfo.saveInBackground(saveCallback);
    }

    public static void getCommentCount(int infoId, CountCallback countCallback) {
        //获取评论条数
        AVQuery<AVObject> query = new AVQuery<>("Commont");
        query.whereEqualTo("infoid", infoId);
        query.countInBackground(countCallback);
    }

    public static void getComment(int id, FindCallback callback) {
        AVQuery<AVObject> commonsInfo = new AVQuery<>("Commont");
        commonsInfo.whereEqualTo("infoid", id);
        // 按时间，降序排列
        commonsInfo.orderByDescending("createdAt");
        commonsInfo.findInBackground(callback);
    }

    /**
     * 获取收藏数据
     *
     * @param page
     * @param username
     */
    public static void getCollection(int page, String username, FindCallback callback) {
        AVQuery<AVObject> collectionInfos = new AVQuery<>(Constants.Collection.FORM_COLLECTION);
        collectionInfos.whereEqualTo(Constants.Collection.USER_ID, username);
        collectionInfos.orderByDescending("createdAt");
        collectionInfos.limit(20);
        collectionInfos.skip((page - 1) * 20);// 跳过 20 条结果
        collectionInfos.findInBackground(callback);
    }

    public static void addMyCommentInfo(DataBean dataBean, String trim, int type) {
        AVObject avObject = new AVObject(Constants.MyComment.FORM_MYCOMMENT);
        avObject.put(Constants.MyComment.VIDEO_INFO, GsonUtils.beanToJson(dataBean));
        avObject.put(Constants.MyComment.COMMENTCONTENT, trim);
        avObject.put(Constants.MyComment.USER_ID, User.getCurrenUser().getUsername());
        avObject.put(Constants.Collection.VIDEO_TYPE, type);
        avObject.put(Constants.Collection.INFOID, dataBean.getId());
        avObject.saveInBackground();
    }

    public static void addMyCommentInfoX(VVIdeoDetailsEntity dataBean, String trim, int type) {
        AVObject avObject = new AVObject(Constants.MyComment.FORM_MYCOMMENT);
        avObject.put(Constants.MyComment.VIDEO_INFO, GsonUtils.beanToJson(dataBean));
        avObject.put(Constants.MyComment.COMMENTCONTENT, trim);
        avObject.put(Constants.MyComment.USER_ID, User.getCurrenUser().getUsername());
        avObject.put(Constants.Collection.VIDEO_TYPE, type);
        avObject.put(Constants.Collection.INFOID, Integer.parseInt(dataBean.getData().getPostid()));
        avObject.saveInBackground();
    }

    public static void getMyCommentInfo(int page, String username, FindCallback callback) {
        AVQuery<AVObject> collectionInfos = new AVQuery<>(Constants.MyComment.FORM_MYCOMMENT);
        collectionInfos.whereEqualTo(Constants.Collection.USER_ID, username);
        collectionInfos.orderByDescending("createdAt");
        collectionInfos.limit(20);
        collectionInfos.skip((page - 1) * 20);// 跳过 20 条结果
        collectionInfos.findInBackground(callback);
    }

    public static void delletCollection(String objectId) {
        AVQuery.doCloudQueryInBackground("delete from " + Constants.Collection.FORM_COLLECTION + "" +
                " where objectId='" + objectId + "'", new CloudQueryCallback<AVCloudQueryResult>() {
            @Override
            public void done(AVCloudQueryResult avCloudQueryResult, AVException e) {
                // 如果 e 为空，说明保存成功
            }
        });

//        AVObject avObject = new AVObject(Constants.Collection.FORM_COLLECTION);
//        avObject.put("objectId", objectId);
//        avObject.deleteInBackground(new DeleteCallback() {
//            @Override
//            public void done(AVException e) {
//                e.printStackTrace();
//            }
//        });
    }
}
